﻿using PLCSharp.Models;
using Prism.Dialogs;
using Prism.Events;
using Prism.Ioc;
using Prism.Mvvm;
using System.ComponentModel.DataAnnotations.Schema;

namespace PLCSharp.CoreModel.Prism
{
    public class ModelBase : BindableBase
    {
        protected readonly IEventAggregator _EventAggregator;
        [NotMapped]
        public readonly IDialogService _dialogService;
        [NotMapped]
        public readonly IContainerExtension _container;
        [NotMapped]
        public DatasContext _DatasContext { get; set; }
        public ModelBase(IContainerExtension container, IEventAggregator ea, IDialogService dialogService)
        {
            _container = container;
            _DatasContext = container.Resolve<DatasContext>();
            _EventAggregator = ea;
            _dialogService = dialogService;
            AppDomain.CurrentDomain.ProcessExit += OnExit;

        }


        protected virtual void OnExit(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 发送消息到事件总线
        /// </summary>
        /// <param name="msg">消息</param>
        /// <param name="type">消息类型  1 记录 2 弹出 3 弹出并记录，4 弹出确认框</param>
        public void SendErr(string msg, ErrType type)
        {
            _EventAggregator.GetEvent<MessageEvent>().Publish(new()
            {
                Type = type,
                Target = "errLog",
                Content = msg
            });
        }
        public void SendErr(string msg)
        {
            SendErr(msg, ErrType.Record);
        }
 
    }
}